import Vue from 'vue'
import Router from 'vue-router'
import Index from './views/Index.vue'
import Login from './views/Login.vue'
import NotFound from './views/NotFound.vue'
import Info from './views/Info.vue'
import Cpassword from './views/Cpassword.vue'
import Register from './views/Register.vue'
import HuanbaoSearch from './views/HuanbaoSearch.vue'
import Tojilist from './views/Tojilist'
import Onlinelist from './views/Onlinelist'
import Usersguanli from './views/Usersguanli'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
            path: '/login',
            component: Login
        },
        {
            path: '/register',
            component: Register
        },

        {
            path: '/',
            component: Index,
            children: [{
                    path: '/tojilist',
                    name: 'tojilist',
                    component: Tojilist
                },
                {
                    path: '/onlinelist',
                    name: 'onlinelist',
                    component: Onlinelist
                },
                {
                    path: '/huanbaoSearch',
                    name: 'huanbaoSearch',
                    component: HuanbaoSearch
                },
                {
                    path: '/404',
                    component: NotFound
                },
                {
                    path: '/info',
                    name: 'info',
                    component: Info
                },
                {
                    path: '/cPassword',
                    name: 'cPassword',
                    component: Cpassword
                },
                {
                    path: '/usersguanli',
                    name: 'usersguanli',
                    component: Usersguanli
                }
            ]
        },
        {
            path: '*',
            redirect: { path: '/404' }
        }
    ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
    const LOGINURL = '/login'
    let url, user

    url = to.path
    user = sessionStorage.getItem('user')

    if (url === LOGINURL) {
        sessionStorage.removeItem('user')
    }

    // 判断是否登录
    if (!user && url !== LOGINURL && url != '/register') {

        next({ path: LOGINURL })

    } else {
        next()
    }
})

export default router